package com.financing.platform.web.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IfpFetchProductRegexService;
import com.financing.platform.util.StringUtil;

@Controller("fpFetchProductRegex")
@RequestMapping("/api/fpFetchProductRegex")
@Scope("prototype")
public class fpFetchProductRegexAction extends BaseAction{
	
	@Autowired
	IfpFetchProductRegexService fpFetchProductRegexService;
	
	@RequestMapping("/queryAllProductRegex.shtml")
	@ResponseBody
	public ResultMap queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			Map<String,String> map=new HashMap<String, String>();
			
			
			List<Map<String, String>> queryAllFetchXpath = fpFetchProductRegexService.queryAllFetchXpath(map);
			resultMap.setAttribute("fetchXpath",queryAllFetchXpath);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("queryAll error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	
	@RequestMapping("/updateFetchProductRegex.shtml")
	@ResponseBody
	public ResultMap updateFetchProductRegex(HttpServletRequest request,
			HttpServletResponse response,String id,String xpathRegex) {
		try{
			boolean b = StringUtil.isNullOrEmptyStr(id)||StringUtil.isNullOrEmptyStr(xpathRegex);
			if(b){
				resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
				return resultMap;
			}
			//更新 规则
			Map<String,String> map=new HashMap<String, String>();
			map.put("id", id);
			map.put("xpathRegex", xpathRegex);
			//设置更新人
			HttpSession session = request.getSession();
			UserInfoDTO userinfo = (UserInfoDTO) session.getAttribute("USER");
			map.put("updateBy", userinfo.getUsername());
			
			fpFetchProductRegexService.updateFetchXpath(map);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("updateFetchProductRegex error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
}
